package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.demo.entity.DaAn;
import com.example.demo.entity.Student;
import com.example.demo.entity.common.CommonResult;
import com.example.demo.listener.DaAnExcelListener;
import com.example.demo.listener.StudentExcelListener;
import com.example.demo.service.DaAnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("daan")
public class DaAnController {
    @Autowired
    DaAnService daAnService;

    @RequestMapping("toList")
    public String toList() {
        return "user/daan_list";
    }

    /**
     * 文件上传
     */
    @RequestMapping("upload")
    @ResponseBody
    public Object upload(MultipartFile file) {
        CommonResult<String> result = new CommonResult<>();
        try {
            EasyExcel.read(file.getInputStream(), DaAn.class, new DaAnExcelListener(daAnService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg("文件上传异常");
        }
        result.setData(file.getOriginalFilename());
        return result;
    }
}
